#include "Shm.hpp"


int main()
{
    key_t key = getkey();
    int shmid = getshm(key, Size);

    std::cout << "client key : " << to_hex(key) << std::endl;
    std::cout << "client shmid : " << shmid << std::endl;
    char *start = (char *)shmat(shmid, NULL, 0); //建立连接
    
    if ((void *)shmat == (void *)-1)
    {
        std::cerr << "shmat fail !!! errno : " << errno << "|" << strerror(errno) << std::endl;
        exit(1);
    }

    printf("start: %p\n", start);
    int fd = open("pipetest",O_WRONLY);
    while (1) //向共享内存写入数据
    {   
        std::cout << "cilent send : ";
        fflush(stdout);//刷新缓冲区
         
        ssize_t s = read(0, start, Size); //向输入流中读取size个字符放入start
        start[s - 1] = 0; //判定最后一个字符"\n"强转为'\0'
        assert(start > 0);//断言
        (void)start;
        char i[4] ={0};
        write(fd,i,sizeof(i)); // 写入管道
        if (strcmp(start, "quit") == 0)
            break;
    }
    sleep(3);
    shmdt(start);
    close(fd);
    // shmctl(shmid, IPC_RMID, NULL);
    // std::cout << "释放成功" << std::endl;
    return 0;
}